package com.padakeji.android.ui.examples;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.padakeji.android.ui.widget.ListViewX;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by atanl on 2015/12/5.
 */
public class ListViewXTestActivity extends Activity {

    private ListViewX mInScrollList;

    private void findViews() {
        mInScrollList = (ListViewX) findViewById(R.id.activity_listViewXTest_list_inScroll);
    }

    // Data.
    private List<String> mData = new ArrayList<>();
    private BaseAdapter mAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public String getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
                vh = new ViewHolder();
                vh.tv = (TextView) convertView.findViewById(android.R.id.text1);
                vh.tv.setTextColor(getResources().getColor(android.R.color.holo_red_light));
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }

            String item = getItem(position);
            vh.tv.setText(item);

            return convertView;
        }
    };

    private class ViewHolder {
        TextView tv;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listviewx_test);

        findViews();
        configViews();

        requestData();
    }

    private void configViews() {
        mInScrollList.enableAutoLoad();
        mInScrollList.setAutoLoadListener(new ListViewX.AutoLoadListener<String[]>() {

            @Override
            public String[] loadInBackground() {
                try {
                    Thread.sleep(2000);
                    return new String[]{times + "-abc", times + "-def", };
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            public void onLoadFinish(String[] data) {
                ++times;
                mData.addAll(Arrays.asList(data));
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLoadFail(Throwable throwable) {

            }
        });
        mInScrollList.setAdapter(mAdapter);
    }

    private int times = 0;
    private void requestData() {
        for (int i = 0; i < 10; ++i) {
            mData.add(times + "-" + i);
        }
        ++times;
    }

}
